<?php 
/**
 * creates layout for account
 * @param 		mixed		 $auth
 * @return		string		 $output
 */
function CreateAccountLayout($auth){
	try {
		$mysql = $auth->GetSqlConnection();
		$account = GetLanguageSpecificText($mysql, "konto_menue2", LANGUAGE);

		$output = '';

		$output .= CreateAccountToolbar($mysql);

		$output .= '<form id="form_create_account" action="' . COSTPOINTS . '" name="form_create_accounts" method="post" style="display:none">';
		$output .= '<table >';
		$output .= '<tr>';
		$output .= '<td><span style="color: #61021B; font-weight:bold">Bank:</span></td>';
		$output .= '<td><input type="text" id="bankName" class="text ui-widget-content ui-corner-all" style="width:200px; height:15px;" /></td>';
		$output .= '</tr>';
		$output .= '<tr>';
		$output .= '<td><span style="color: #61021B; font-weight:bold">' . $account . ':</span></td>';
		$output .= '<td><input type="text" id="accountName" class="text ui-widget-content ui-corner-all" style="width:200px; height:15px;" /></td>';
		$output .= '</tr>';
		$output .= '</table>';
		$output .= '</form>';

		$output .= CreateAccountTable($auth, $account);

		return $output;
	} catch (Exception $e) {
		return $e->getMessage();
	}
}


/**
 * creates toolbar for account
 * @param 		mixed		 $mysql
 * @return		string		 $output
 */
function CreateAccountToolbar($mysql){
	$output = '';

	$new = GetLanguageSpecificText($mysql, "new_attribute", LANGUAGE);
	$save = GetLanguageSpecificText($mysql, "save_attribute", LANGUAGE);

	$output .= '<span id="itemtoolbar" class="ui-widget-header ui-corner-all">';
	$output .= '<button id="btnNew" onclick="HandleNewAccount();">' . $new . '</button>';
	$output .= '<button id="btnSave" onclick="HandleSaveAccount();">' . $save . '</button>';
	$output .= '</span><br /><br />';

	return $output;
}


/**
 * creates output table for categories
 * @param 		mixed		 $mysql			sql connection
 * @param 		string		 $description	description header for table accordingly the language
 */
function CreateAccountTable($auth, $account) {
	$output = '';

	$output .= '<div style="margin-top:40px" id="account_table">';
	$output .= '<table class="outputTable" style="width:600px">';
	$output .= '<tr>';
	$output .= '<th>&nbsp;</th>';

	$output .= '<th>Bank</td>';
	$output .= '<th>' . $account . '</th>';
	$output .= '</tr>';

	$sql = 'select * from ' . DB . '.Konto where person_id=? order by bank, name';
	$params = array($auth->GetUserId());

	try {
		$mysql = $auth->GetSqlConnection();
		$mysql->Execute($sql, $params);

		while (($account = $mysql->Fetch())) {
			$output .= '<tr><td style="width:35px">' . GenerateUIIcon('ui-icon-pencil', NULL, 'HandleEditAccount(' . $account->konto_id . ');') . GenerateUIIcon('ui-icon-trash', NULL, 'DeleteAccount(' . $account->konto_id . ');') . '</td><td>' . $account->bank . '</td>';
			$output .= '<td>' . $account->name . '</td></tr>';
		}

	} catch (Exception $e) {
		return $e->getMessage();
	}

	$output .= '</table>';
	$output .= '</div>';

	return $output;
}


/**
 * saves account
 * @param 		int			 $accountId
 * @param 		mixed		 $auth
 */
function SaveAccount($accountId = NULL, $auth){
	$type = (isset($_REQUEST['TYPE'])) ? $_REQUEST['TYPE'] : NULL;
	$accountName = (isset($_REQUEST['ACCOUNTNAME'])) ? $_REQUEST['ACCOUNTNAME'] : NULL;
	$bankName = (isset($_REQUEST['BANKNAME'])) ? $_REQUEST['BANKNAME'] : NULL;
	$personId = $auth->GetUserId();
	$mysql = $auth->GetSqlConnection();

	switch($type){
		
		case 'NEW':
			try{
					
				$accountId = GetNextID($mysql, 'Konto', 'konto_id');
					
				$sql = 'insert into ' . DB . '.Konto (konto_id, bank, name, person_id) values (?, ?, ?, ?)';
				$params = array(
						$accountId,
						$bankName,
						$accountName,
						$personId);
					
				$mysql->Execute($sql, $params);
				$result = array(
						'RESPONSE' => 'SUCCESS', 'ACCOUNT_ID' => $accountId);
			} catch (Exception $e){
				$result = array(
						'RESPONSE' => 'ERROR',
						'MESSAGE' => $e->getMessage());
			}
			break;

		case 'EDIT':
			try{
				$sql = 'update ' . DB . '.Konto set bank=?, name =? where konto_id=? and person_id=?';
				$params = array (
						$bankName,
						$accountName,
						$accountId,
						$personId);

				$mysql->Execute($sql, $params);
				$result = array(
						'RESPONSE' => 'SUCCESS', 'ACCOUNT_ID' => accountId);
			} catch (Exception $e){
				$result = array(
						'RESPONSE' => 'ERROR',
						'MESSAGE' => $e->getMessage());
			}
			break;
	}

	echo json_encode($result);
}



/**
 * reloads the category table after deleating or saving
 * @param 		mixed		$auth
 * @return		string		$output
 */
function ReloadAccountTable($auth) {
	$output = '';
	try {
		$mysql = $auth->GetSqlConnection();
		$account = GetLanguageSpecificText($mysql, "konto_menue2", LANGUAGE);
		$output = CreateAccountTable($auth, $account);
	} catch (Exception $e) {
		$output = $e->getMessage();
	}

	return $output;
}


function GetAccountData($accountId, $auth) {
	if (isset($accountId)) {
		try {
			$mysql = $auth->GetSqlConnection();

			$sql = 'select name, bank from ' . DB . '.Konto where konto_id=?';
			$params = array(
					$accountId);

			$mysql->Execute($sql, $params);
			$account = $mysql->Fetch();
			$result = array(
					'RESPONSE' => 'SUCCESS',
					'BANKNAME' => $account->bank,
					'ACCOUNTNAME' => $account->name);

		} catch (Exception $e) {
			$result = array(
					'RESPONSE' => 'ERROR',
					'MESSAGE' => $e->getMessage());
		}
	} else {
		$result = array(
				'RESPONSE' => 'ERROR',
				'MESSAGE' => 'AccountId is not set!');
	}
	echo json_encode($result);
}


/**
 * deletes category
 * @param 		int		$categoryId
 * @return		array	$result
 */
function DeleteAccount($accountId, $auth) {
	if (isset($accountId)) {
		try {
			$mysql = $auth->GetSqlConnection();
				
			$sql = 'delete from ' . DB . '.Konto where konto_id=?';
			$params = array(
					$accountId);
				
			$mysql->Execute($sql, $params);
				
			$result = array(
					'RESPONSE' => 'SUCCESS');

		} catch (Exception $e) {
			$result = array(
					'RESPONSE' => 'ERROR',
					'MESSAGE' => $e->getMessage());
		}
	} else {
		$result = array(
				'RESPONSE' => 'ERROR',
				'MESSAGE' => 'AccountId is not set!');
	}

	echo json_encode($result);
}
?>